/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.deposit;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import org.primefaces.context.RequestContext;
import src.domain.deposit.Bilyet;
import src.domain.saving.Saving;
import src.domain.security.Users;
import src.services.deposit.BilyetModel;
import src.services.deposit.DeproductModel;
import src.services.saving.SavingModel;
import src.util.Constanta;
import src.util.Messager;
import src.view.security.LoginBean;
import src.view.system.ApprovalBean;

/**
 *
 * @author Yusup Jauhari Shandi <in.visi>
 */

@ManagedBean
@SessionScoped
public class BilyetapprovalBean implements Serializable{
    private Bilyet bilyet;
    private List<Bilyet> bilyets;
    private BilyetModel bilHelper;
    private List<SelectItem> avblProd, avblDisb;
    private String accntDest, bilyetParentNo, accntOwner;
    private Users user;
    private String depSavaccntNO, depSavaccntOwner;
    private BigDecimal saldo;
    private ApprovalBean approvalBean;
    
    public BilyetapprovalBean() {
        ambilUser();
        this.bilHelper = new BilyetModel();
        this.avblDisb = Constanta.getDepDisb();
        FacesContext context = FacesContext.getCurrentInstance();
        approvalBean = (ApprovalBean) context.getApplication().evaluateExpressionGet(context, "#{approvalBean}", ApprovalBean.class);
        approvalBean.setUpdateTab(false);
    }
    
    private void ambilUser() {
        FacesContext context = FacesContext.getCurrentInstance();
        LoginBean logBean = (LoginBean) context.getApplication().evaluateExpressionGet(context, "#{loginBean}", LoginBean.class);
        this.user = logBean.getSelectedUser();
    }
    
    public Bilyet getBilyet() {
        return bilyet;
    }

    public void setBilyet(Bilyet bilyet) {
        this.bilyet = bilyet;
    }
    
    public String getAccntDest() {
        return accntDest;
    }

    public String getBilyetParentNo() {
        return bilyetParentNo;
    }

    public String getAccntOwner() {
        return accntOwner;
    }

    public List<SelectItem> getAvblDisb() {
        return avblDisb;
    }

    public List<SelectItem> getAvblProd() {
        return avblProd;
    }

    public List<Bilyet> getBilyets() {
        return bilyets;
    }
        
    public void initForm(){
        this.avblProd = new DeproductModel().listAll();
        
    }
    
    public final void loadSavings () {
        Map<String, Object> savfilter = new HashMap<>();
        Map<Object, Object> amtfilter = new HashMap<>();
        amtfilter.put("<=", user.getUgroup().getDepMaxDeposit());
        savfilter.put("authorizedUid", null);
        savfilter.put("closeDate", null);
        savfilter.put("principleAmount", amtfilter);
        this.bilyets = bilHelper.findAll(savfilter, " order by bilyetDate");
    }
    
    public void openData(){
        approvalBean.setUpdateTab(false);
        initForm();
        if (bilyet.getSavaccntId() != null) {
            Saving sav = new SavingModel().getById(bilyet.getSavaccntId());
            accntDest = sav.getAccountNo();
            accntOwner = sav.getCustomer().getCustomerName();
        }
        
        if (bilyet.getDepositSavaccntId() != null) {
            Saving sav = new SavingModel().getById(bilyet.getDepositSavaccntId());
            depSavaccntNO = sav.getAccountNo();
            depSavaccntOwner = sav.getCustomer().getCustomerName();
            saldo = sav.getBalance();
        }
        
        if (bilyet.getParentBilyetId() != null) {
            Bilyet bil = bilHelper.getById(bilyet.getSavaccntId());
            bilyetParentNo = bil.getBilyetNo();
        }
    }    
    
    public void approve(boolean approved) {
        RequestContext context = RequestContext.getCurrentInstance();
        boolean success;  
        
        bilyet.setAuthorizedUid(user.getUserId());
        if (approved==false) bilyet.setCloseDate(new Date());
        
        success = bilHelper.saveData(bilyet);
        
        if (success) {
            this.bilyets.remove(this.bilyet);
            this.bilyet = null;
            approvalBean.updateTabOtor();
        } 
        
        Messager.showSaveUpdateStatus(success);     
        context.addCallbackParam("success", success);  
    }

    public String getDepSavaccntNO() {
        return depSavaccntNO;
    }

    public String getDepSavaccntOwner() {
        return depSavaccntOwner;
    }

    public BigDecimal getSaldo() {
        return saldo;
    }
    
}
